package com.meshnetics.gb.type.impl.zcl;

import com.meshnetics.gb.type.DataType;
import com.meshnetics.gb.type.DataValue;

/**
 * Abstract implementation of DataValue. Hold data type only. Provides equals(Object) based on type and class checking
 * and hash code based on type. toString(String) calls mandatory formatter for short log string representation.
 * <br>
 * Intended for extending and defining of serialization methods.
 *
 * @author AKovylin
 */
public abstract class AbstractDataValue implements DataValue, ZCLInvalidable {
    protected final DataType dataType;

    protected AbstractDataValue(DataType type) {
        this.dataType = type;
    }

    public DataType getType() {
        return dataType;
    }

    public String stringValue() {
        return dataType.getMandatoryFormatter().formatSerializedString(this);
    }

    public boolean equals(Object obj) {
        return obj instanceof AbstractDataValue &&
                this.dataType.equals(((AbstractDataValue) obj).dataType);
    }

    public int hashCode() {
        return dataType.hashCode();
    }

    public String toString() {
        return dataType.getMandatoryFormatter().formatShortLogString(this);
    }
}
